package cn.itsy.dao.dept;

import cn.itsy.pojo.Dept;
import cn.itsy.pojo.EmpAndDept;
import cn.itsy.pojo.JobAndEmpAndDept;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "deptDaoimpl")
public class DeptDaoimpl implements DeptDao {
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    /**
     * 添加部门的实现方法
     *
     * @param dept
     * @return
     */
    @Override
    public Integer insertDept(String sql, Dept dept) {
//        int ran = (int) (Math.random()*99999+1);
//        dept.setId(ran);
        dept.setStatus("Y");
        int update = this.jdbcTemplate.update(sql, dept.getId(), dept.getDbian(), dept.getDname(), dept.getPid(),
                dept.getDmanager(), dept.getDtel(), dept.getDfax(), dept.getDemail(),
                dept.getDuty(), dept.getStatus());
        return update;
    }

    @Override
    public List<Dept> queryAll(String sql) {
        List<Dept> query = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<Dept>(Dept.class));
        return query;
    }

    @Override
    public List<Dept> queryDeptById(String sql, Integer id) {
        List<Dept> query = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<Dept>(Dept.class), id);
        return query;
    }

    @Override
    public Integer updateDeptById(String sql, Dept dept) {
        int update = this.jdbcTemplate.update(sql, dept.getDname(), dept.getDmanager(), dept.getDtel(),
                dept.getDfax(), dept.getDemail(), dept.getDuty(), dept.getId());
        return update;
    }

    @Override
    public Integer updateDeptById(String sql, String dbian, Integer id) {
        int update = this.jdbcTemplate.update(sql, dbian, id);
        return update;
    }

    @Override
    public Integer updateDeptById(String sql, Integer pid, Integer id) {
        int update = this.jdbcTemplate.update(sql, pid, id);
        return update;
    }


    @Override
    public void deleteDeptById(String sql, Integer idValue) {
        this.jdbcTemplate.update(sql, idValue);
    }

    @Override
    public List<EmpAndDept> queryEmpAndDeptById(String sql, Integer depart_id) {
        List<EmpAndDept> query = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<EmpAndDept>(EmpAndDept.class), depart_id);
        return query;
    }

    @Override
    public List<JobAndEmpAndDept> queryDeptsById(String sql, Integer... id) {
        List<JobAndEmpAndDept> query = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<JobAndEmpAndDept>(JobAndEmpAndDept.class), id[0], id[1]);
        return query;
    }

    @Override
    public List<Dept> queryLastID(String sql) {
        List<Dept> query = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<Dept>(Dept.class));
        return query;
    }

    @Override
    public boolean updateEmpAndJobById(String sql, Integer... id) {
        int update = this.jdbcTemplate.update(sql, id[0], id[1], id[2]);
        if (update != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateJobById(String sql, Integer... id) {
        int update = this.jdbcTemplate.update(sql, id[0], id[1], id[2]);
        if (update != 0) {
            return true;
        }
        return false;
    }

    @Override
    public void deleteDeptByIds(String sql, Integer... id) {
        this.jdbcTemplate.update(sql, id[0], id[1]);
    }
}
